<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: check_licence.php 225 2013-09-22 10:21:51Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 225 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 10:21:51 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */
?>
<a id="update_check" name="update_check"></a>
<table width="100%" class="dataTable">
	<?php echo getDataFromMasterServer(); ?>
</table><br />
<?php 
if(isset($_GET['action']) && $_GET['action'] == 'check') {
	$params = 'mail='.$_POST['mail'].'&key='.$_POST['key'].'&token='.sha1($_POST['mail'].$_POST['key']).'&vn='.PROJECT_VERSION_PLAIN;
	echo '<table width="100%" class="dataTable">'.getDataFromMasterServer($params).'</table>';
} else {
?>
<form action="start.php?action=check#update_check" method="POST" name="validate_user">
	<table width="100%" class="dataTable">
		<tr>
			<td colspan="2">
			<?php echo TEXT_WIDGET_CL_INFO ?>
			</td>
		</tr>
		<tr>
			<td><?php echo TEXT_WIDGET_CL_EMAIL ?></td>
			<td><input autocomplete="off" type="text" name="mail" style="width:99%" /></td>
		</tr>
		<tr>
			<td>Lizenz-Schlüssel:</td>
			<td><input autocomplete="off" type="text" name="key" style="width:99%" /></td>
		</tr>
		<tr>
			<td colspan="2">
				<?php echo TEXT_WIDGET_CL_SSL ?>			
				<button class="button" type="submit">
				<?php echo TEXT_WIDGET_CL_CHECK ?>	
				</button>
			</td>
		</tr>
	</table>
</form>
<?php } 

function getDataFromMasterServer($params = '') {
	if(empty($params))
		$params = 'check=new_version&vn='.PROJECT_VERSION_PLAIN.'&vt='.urlencode(PROJECT_VERSION_TYPE);
    //https://www.seo-mercari.de/check.php?check=new_version&vn=1.4.3&vt=Pro+Edition

	if(function_exists('curl_init')) {
		$url = 'https://www.seo-mercari.de/check.php?'.$params;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$output = curl_exec($ch);
		curl_close($ch);
		if(!empty($output))
			return $output;
		else
			return 'Der Server konnte nicht erreicht werden.';
		
	} elseif(ini_get(allow_url_fopen) !== false && function_exists(file_get_contents)) {
		if(in_array('https', stream_get_wrappers()))
			$data = file_get_contents('https://www.seo-mercari.de/check.php?'.$params);
		else
			$data = file_get_contents('http://www.seo-mercari.de/check.php?'.$params);

		if(!empty($data))
			return $data;
		else
			return 'Der Server konnte nicht erreicht werden.';
		
	} elseif(function_exists('openssl_open')) {
		$host = 'ssl://www.seo-mercari.de';
		$uri  = '/check.php?'.$params;
	
		header("Content-type: text/plain");
		$sock = fsockopen($host, 443, $errno, $errstr, 5);
		fputs($sock, "GET ".$uri." HTTP/1.1\r\n");
		fputs($sock, "Host: ".$host."\r\n");
		fputs($sock, "Connection: close\r\n\r\n");
		$result = array();
		/*while(!feof($sock))
			$result[] = fgets($sock, 4096);*/
		fclose($sock);
		if(!empty($result['9'])) {
			for($i = 9, $size = sizeof($result); $i < $size; ++$i) {
				if(!empty($result[$i]) || $result[$i] !='0')
					$echo .= $result[$i];
			}
			return $echo;
			
		} else 
			return 'Sie konnten nicht authentifiziert werden.';
	}
}
?>